<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>修改用户信息</title>
</head>
<link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
<link rel="stylesheet" href="/css/common.css" media="all">
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js" ></script>
<body>
<style type="text/css">
    .formbox {
        height: 1px;
    }

    body {
        background-color: white;
    }
</style>
<div class="layui-row">
    <div class="layui-col-md2  formbox ">
    </div>

    <div class="layui-fluid layui-col-md8 layui-col-sm12 ">
        <div class="layui-card">
            <div class="layui-card-body" style="padding: 15px;" class="layui-row">
                <form class="layui-form" action="" lay-filter="selFilter">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color:red">*</span>负责人姓名:</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" required lay-verify="name" placeholder="请输入负责人姓名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color:red">*</span>头像:</label>
                        <div class="layui-input-block">
                            <img id="headImg" src="" width="150px" height="100px"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color:red">*</span>联系电话:</label>
                        <div class="layui-input-block">
                            <input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" name="adminPhone"
                                   id="adminPhone" required
                                   lay-verify="adminPhone" placeholder="请输入电话号码" autocomplete="off" class="layui-input"
                                   maxlength="11">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color:red">*</span>密码:</label>
                        <div class="layui-input-block">
                            <input type="password" name="adminPassword" id="adminPassword" required
                                   lay-verify="adminPassword"
                                   placeholder="请输入密码" autocomplete="off" class="layui-input" readonly='readonly'>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color:red">*</span>昵称:</label>
                        <div class="layui-input-block">
                            <input type="text" name="adminName" id="adminName" required lay-verify="adminName"
                                   placeholder="请输入用户昵称"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label"><span style="color:red">*</span>角色:</label>-->
                        <!--<div class="layui-input-block" lay-filter="selFilter">-->
                            <!--<select name="server" id="server">-->
                                <!--<option value="">请选择角色</option>-->
                            <!--</select>-->
                        <!--</div>-->
                    <!--</div>-->
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color:red">*</span>用户性质:</label>
                        <div class="layui-input-block  layui-form" lay-filter="selFilter">
                            <select name="xinzhi" lay-verify="xinzhi" id="xinzhi">
                                <option value=""></option>
                                <option value="0">个人</option>
                                <option value="1">企业</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0">
                                <button class="layui-btn" lay-filter="demo1" lay-submit style="margin-left:-11px">修改
                                </button><!---->
                                <button class="layui-btn layui-icon layui-icon-close" id="back">
                                    取消
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-col-md2 formbox">

            </div>
        </div>
    </div>
</div>

</body>
</html>

<script>
        var fileName = null;
        var $ = layui.$;
        var s = layui.data("xiugai");
        var laydate = layui.laydate
        var form = layui.form
        $("#name").val(s.admindata.admin_person_in_charge);
        $("#adminPhone").val(s.admindata.admin_phone);
        $("#adminName").val(s.admindata.admin_name);

        $("#adminPhone").blur(function () {

            var reg = /^1[34578]\d{9}$/;
            if (!reg.test($("#adminPhone").val())) {
                $("#iphonered").html("*手机号码格式不正确")
            } else {
                $("#iphonered").html("")
            }
        });

        var kaiguan = "";
        var adminid = 0;
        $(function () {
            $.ajax({
                url: "/admin/updateselct",
                data: {
                    adminName: s.admindata.admin_name
                },
                type: "GET",
                dataType: "json",
                success: function (result) {
                    kaiguan = "";
                    adminid = 0;
                    // alert(result.data[0].role_id)
                    $("#adminPassword").val(result.data[0].admin_password);

                    fileName=result.data[0].admin_head_portrait;
                    var imageUrl = "http://127.0.0.1:8080/images/" + result.data[0].admin_head_portrait;
                    $("#headImg").attr("src", imageUrl);
                    kaiguan = result.data[0].admin_type;
                    adminid = result.data[0].admin_id;
                    $("#xinzhi").val(kaiguan == "个人" ? 0 : 1);
                    form.render('select', 'selFilter')
                }
            })

        })

        var upload = layui.upload;
        upload.render({
            elem: '#headImg',
            url: '/admin/fileinfo/upload/image',
            choose: function (obj) {
                fileName = null;
                //预览
                obj.preview(function (index, file, result) {
                    document.getElementById("headImg").src = result;
                });
            },
            done: function (res, index, upload) //上传完成
            {
                fileName = res.data; //传回来的新的文件名
            }
        });


        form.on('submit(demo1)', function (data) {
            var re = /^1[34578]\d{9}$/;
            if ($("#name").val() == "") {
                layer.msg("请填写负责人姓名", {icon: 2});
                return false;
            }
            if (re.test($("#adminPhone").val()) == false) {
                layer.msg("请确认电话号码", {icon: 2});
                return false;
            }

            if ($("#adminName").val() == "") {
                layer.msg("请输入昵称", {icon: 2});
                return false;
            }

            if ($("#xinzhi").val() == "") {
                layer.msg("请选择用户性质", {icon: 2});
                return false;
            }
            if (fileName == null) {
                layer.msg("请先选择图像", {icon: 2});
                return false;
            }

            // if ($("#server").val() == "") {
            //     layer.msg("请选择角色", {icon: 2});
            //     return false;
            // }
            $.ajax({
                type: 'post',
                url: '/admin/updataAdmin',
                data: {
                    adminId: adminid,
                    adminPersonInCharge: $("#name").val(),
                    adminPhone: $("#adminPhone").val(),
                    adminPassword: $("#adminPassword").val(),
                    adminName: $("#adminName").val(),
                    adminType: $("#xinzhi option:selected").val(),
                    adminHeadPortrait: fileName
                },
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 1000
                        })
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);    //关闭弹出层
                        }, 1000)
                        return
                    }
                    layer.msg(res.msg, {
                        icon: 2
                    })
                },
                error: function () {
                    layer.msg('网络原因修改失败', {
                        icon: 2
                    })
                }
            })
            return false
        })

        $('#back').click(function () {
            var index = parent.layer.getFrameIndex(window.name) //获取窗口索引
            parent.layer.close(index)    //关闭弹出层
        })
</script>